package com.newgd.blockchain.demo.controller;

import com.newgd.blockchain.demo.model.Trades;
import com.newgd.blockchain.demo.service.ITradesService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by Niki on 2018/4/14 9:21
 */
@RestController(value = "/trades")
public class LocalbitcoinsController {
    private static final Logger LOGGER = LoggerFactory.getLogger(LocalbitcoinsController.class);
    @Resource
    ITradesService tradesService ;

    /*默认查询时间间隔，默认五分钟*/
    private static final long DEFAULT_QUERY_INTERVAL = 5 * 60 * 1000 ;

    // TODO: 2018/4/14 添加接口返回对象，不直接返回model
    // TODO: 2018/4/14 添加统一的异常处理，并转换为异常码以及异常信息返回给客户端
    @GetMapping("/list/{begin}/{end}")
    public List<Trades> listTrades(@PathVariable Long begin, @PathVariable Long end){
        LOGGER.info("查询日期为：{}到{}的交易量", begin,end);
        if(begin == null && end == null){
            end = System.currentTimeMillis();
            begin = end - DEFAULT_QUERY_INTERVAL ;
        }

        if(begin > end){
            begin =  end - DEFAULT_QUERY_INTERVAL ;
        }

        return tradesService.findTradesByDate(begin, end);
    }



}
